package com.example.user.customviewdemo.drawing.xfermode;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * Created by user on 2016/5/6.
 * 利用PorterDuffXfermod.Mode.DST_IN制作的波纹签名效果
 * 原理：
 * 1、一个只有文字签名，其他地方为空白像素的bitmap
 * 2、一个具有波浪效果的bitmap
 */
public class WaveNameView extends View {
    private int offset = 0;//波浪偏移量
    private Bitmap dstBmp, srcBmp;
    private Paint paint,srcPaint,dstPaint;
    private Path path;

    public WaveNameView(Context context) {
        super(context);
    }

    public WaveNameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        path = new Path();

        //srcBmp
        srcPaint = new Paint();
        srcPaint.setTextAlign(Paint.Align.CENTER);
        srcPaint.setTextSize(200);
        srcPaint.setColor(Color.WHITE);
        srcPaint.setAntiAlias(true);
        srcPaint.setStyle(Paint.Style.STROKE);
        srcPaint.setStrokeWidth(10);

        //dstPaint
        dstPaint = new Paint();
        dstPaint.setAntiAlias(true);
        dstPaint.setStyle(Paint.Style.FILL);
        dstPaint.setColor(Color.GREEN);


        paint = new Paint();
        paint.setAntiAlias(true);



        startAnimator(1000);

    }

    public WaveNameView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(0xff293134);

        dstBmp = mkDstBmp(1000, 800);
        srcBmp = mkSrcBmp(1000, 800);

        canvas.drawBitmap(srcBmp, 0, 0, paint);//此处一定要先绘制一次srcBitmap，不然只能显示下半部分

        int layerID = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);
        canvas.drawBitmap(dstBmp, 0, 0, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        canvas.drawBitmap(srcBmp, 0, 0, paint);
        paint.setXfermode(null);
        canvas.restoreToCount(layerID);
    }

    /**
     * 获取源图像(源图像是一个绘制有文字，其他地方为空白像素的bitmap)
     * @param width
     * @param height
     * @return
     */
    private Bitmap mkSrcBmp(int width, int height) {
        Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bm);
        c.drawText("BruceYang", bm.getWidth() / 2, bm.getHeight() / 2 + 80, srcPaint);

        return bm;
    }

    /**
     * 生成目标图像（目标图像是一个具有波浪形的Bitmap）
     * @param width
     * @param height
     * @return
     */
    private Bitmap mkDstBmp(int width, int height) {
        path.reset();
        int waveLenth = width;
        int waveheight = 50;
        int orgX = -width;
        int orgY = height / 2;
        Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);


        path.moveTo(orgX + offset, orgY);
        for (int i = -waveLenth; i <= waveLenth; i += waveLenth) {
            path.rQuadTo(waveLenth / 4, -waveheight, waveLenth / 2, 0);
            path.rQuadTo(waveLenth / 4, waveheight, waveLenth / 2, 0);
        }
        path.lineTo(width, height);
        path.lineTo(-width, height);
        path.close();

        Canvas c = new Canvas(bm);
        c.drawPath(path, dstPaint);

        return bm;


    }

    private void startAnimator(int waveLenth) {
        ValueAnimator animator = ValueAnimator.ofInt(0, waveLenth);
        animator.setInterpolator(new LinearInterpolator());
        animator.setDuration(2000);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                offset = (int) animation.getAnimatedValue();
                postInvalidate();
            }
        });
        animator.start();
    }
}
